package com.cyj.dream.middleware.blackwhitelist.persistence.manager;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cyj.dream.middleware.blackwhitelist.entity.SysBlackWhite;
import com.cyj.dream.middleware.blackwhitelist.persistence.service.ITbSysBlackWhiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Description: 黑白名单服务管理层
 * @BelongsProject: DreamChardonnay
 * @BelongsPackage: com.cyj.dream.middleware.blackwhitelist.persistence.manager
 * @Author: ChenYongJia
 * @CreateTime: 2021-09-15 14:27
 * @Email: chen87647213@163.com
 * @Version: 1.0
 */
@Slf4j
@Component
public class SysBlackWhiteManager {

    @Autowired
    private ITbSysBlackWhiteService iTbSysBlackWhiteService;

    /**
     * 查询全部黑白名单信息
     *
     * @return boolean
     * @date 2021-9-15 14:31
     * @author ChenYongJia
     * @version 1.0
     */
    public List<SysBlackWhite> getAllSysBlackWhite(){
        log.info("进入 SysBlackWhiteManager 查询全部黑白名单信息方法");
        return iTbSysBlackWhiteService.list();
    }

    /**
     * 根据sysBlackWhiteId查询黑白名单信息
     *
     * @param sysBlackWhiteId 黑白名单id
     * @return boolean
     * @date 2021-9-15 14:31
     * @author ChenYongJia
     * @version 1.0
     */
    public SysBlackWhite getSysBlackWhiteById(Long sysBlackWhiteId) {
        log.info("进入 SysBlackWhiteManager 根据sysBlackWhiteId查询黑白名单信息方法，参数 sysBlackWhiteId 为：{}", sysBlackWhiteId);
        LambdaQueryWrapper<SysBlackWhite> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysBlackWhite::getSysBlackWhiteId, sysBlackWhiteId);
        wrapper.eq(SysBlackWhite::getDelFlag, 0);
        return iTbSysBlackWhiteService.getOne(wrapper);
    }

    /**
     * 添加黑白名单信息
     *
     * @param sysBlackWhite 黑白名单对象
     * @return boolean
     * @date 2021-9-15 14:31
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean insertSysBlackWhite(SysBlackWhite sysBlackWhite) {
        log.info("进入 SysBlackWhiteManager 添加黑白名单信息方法，参数 sysBlackWhite 为：{}", JSONObject.toJSONString(sysBlackWhite));
        try {
            return iTbSysBlackWhiteService.save(sysBlackWhite);
        } catch (Exception e) {
            log.error("调用添加黑白名单信息方法失败：{}", e);
            return false;
        }
    }

    /**
     * 修改黑白名单信息
     *
     * @param sysBlackWhite 黑白名单对象
     * @return boolean
     * @date 2021-9-15 14:31
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean updateSysBlackWhite(SysBlackWhite sysBlackWhite) {
        log.info("进入 SysBlackWhiteManager 添加黑白名单信息方法，参数 sysBlackWhite 为：{}", JSONObject.toJSONString(sysBlackWhite));
        try {
            return iTbSysBlackWhiteService.save(sysBlackWhite);
        } catch (Exception e) {
            log.error("调用修改黑白名单信息方法失败：{}", e);
            return false;
        }
    }

    /**
     * 删除黑白名单信息
     *
     * @param sysBlackWhite 黑白名单对象
     * @return boolean
     * @date 2021-9-15 14:31
     * @author ChenYongJia
     * @version 1.0
     */
    public boolean delSysBlackWhite(List<SysBlackWhite> sysBlackWhite) {
        log.info("进入 SysBlackWhiteManager 删除黑白名单信息方法，参数 sysBlackWhite 为：{}", JSONObject.toJSONString(sysBlackWhite));
        try {
            sysBlackWhite.stream().forEach(item->{
                item.setDelFlag(1);
            });
            return iTbSysBlackWhiteService.updateBatchById(sysBlackWhite);
        } catch (Exception e) {
            log.error("删除黑白名单信息方法失败：{}", e);
            return false;
        }
    }

}
